<?php

namespace App\Http\Controllers;

use App\Services\TestServer;
use Illuminate\Http\Request;

class TestController extends Controller
{
    protected $test;
    protected $request;

    public function __construct(TestServer $test, Request $request)
    {
        $this->test = $test;
        $this->request = $request;
    }

    /**
     * 记录列表
     *
     * @param null $keyword
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function view()
    {
        $lists = $this->test->get();

        return view('test.view', [
            'lists' => $lists,
        ]);
    }


    /**
     * post提交
     *
     * @return bool|\Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     */
    public function post()
    {
        $this->validate($this->request, [
            'name' => 'required',
            'status' => 'required|int',
        ]);

        try{
            $this->test->create($this->request->all());
        } catch (\Exception $e) {
            return response($e->getMessage());
        }

        return redirect()->route('test_post');
    }
}